import bonetime from 'bone-time'
import com from './addon/com'
import urly from './addon/urly'

var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'LayoutSideMenu',
	'TabsSide',
	'TabsItemSide',
	'Icon',
	'List',
	'ListItem',
	'Product',
	'ShoppingDock',
	'GridRow',
	'GridCol',
	'MenuSquare'
])

Vue.filter('cut', function (value) {
  return value.substr(0, 30)+' ...';
})

//cvxf
new Vue({
	el: '#app',
	components: components,
	data (){
		return {
			data: [],
			week: [],
			today: 1,
			tabs: [],
			currentDate: '',
			currentWeekDay: '',
			request: null,
			total: TOTAL,
			types: {
				1: '早餐',
				2: '中餐',
				3: '晚餐'
			},
			has: {
				breakfast: false,
				lunch: true,
				dinner: true
			}
		}
	},
	ready: function(){
		var url = new urly(window.location.href);

		var ddd = new Date(DATE[0]);
		this.today = ddd.getDay();
 		if(url.getParams().today){

			this.today = url.getParams().today;
		}
		var today = new Date();
		var weekday = today.getDay();//星期几
		var date = today.getDate();//日
		var haha = ['一', '二', '三', '四', '五', '六', '日']
		if(weekday==0){
			weekday = 7;
		}

		//判断某天是否在要显示的日期内
		/*
		function isIn(keyday){
			var flag = false;
			for(var i=0; i<DATE.length; i++){
				var day = DATE[i].split('-');
				var year1 = keyday.getFullYear();
				var mon1 = keyday.getMonth()+1;
				var day1 = keyday.getDate();

				var year2 = parseInt(day[0]);
				var mon2 = parseInt(day[1]);
				var day2 = parseInt(day[2]);

				if(year1==year2 && mon1==mon2 && day1==day2)
					flag = true;
			}
			return flag;
		}*/


		for(var i=0; i<DATE.length; i++){
			var today2 = new Date();
			this.week.push(DATE[i]);

			var disable = false;
			//var tag = mon+'月'+today2.getDate()+'日';
			var bt = new bonetime(DATE[i] + ' 00:00:00')
			var tag = bt.format('MM月dd日')

			var current = false;
			if(i==0){
				current = true;
			}

			this.tabs.push({
				id: i+1,
				content: '周 '+haha[bt.obj.getDay()-1],
				tag: tag,
				type: 'tag',
				current: current,
				disable: disable
			});
			if(current){
				this.currentDate = DATE[0];//today2.getFullYear()+'-'+mon+'-'+today2.getDate();
			}
		}

		/*
		for (var i = 0; i<7; i++){
			var today2 = new Date();

			today2.setDate(date-weekday+1+i+7);
			var mon = today2.getMonth()+1;

			this.week.push(today2.getFullYear()+'-'+mon+'-'+today2.getDate());
			//
			 if(!isIn(today2)){

			 	continue;
			 }
			//


			var current = false;
			if(this.today == today2.getDay()){
				current = true;
			}
			var disable = false;
			if(weekday > today2.getDay() && today2.getDay() != 0){
				disable = false;
			}
			var tag = mon+'月'+today2.getDate()+'日';
			//if(weekday == today2.getDay()){
				//tag = '今 天';
			//}

			this.tabs.push({
				id: i+1,
				content: '周' + ' '+ haha[i],
				tag: tag,
				type: 'tag',
				current: current,
				disable: disable
			});

			if(current){
				this.currentDate = today2.getFullYear()+'-'+mon+'-'+today2.getDate();
			}
		}*/


		this.loading(1);

	},
	events: {
		'tab-item-click': function(msg){
			this.changeDate(msg);
		}
	},
	methods: {
		goDetail: function(pro){
			window.location.href = SITE_URL + '/home/get_productdetail/id/' + pro.data.id + '/date/' + this.currentDate + '?type=' + pro.data.type;//+ '?date=' + this.currentDate;
		},
		changeDate: function(weekday){

			this.loading(weekday);
		},
		loading: function(weekDay){

			if(this.request){
				this.request.abort();
			}
			this.currentWeekDay = weekDay;
			var mylist = this.$refs.mylist;
			var _this = this;
			this.request = $.ajax({
				url: SITE_URL + '/home/get_products',
				data: {
					date: _this.week[weekDay-1]
				},
				type: 'post',
				success: function(data){

					mylist.setData(data, 'id');

					for(var key in data){
						if(data[key].type == 1){
							_this.has.breakfast = true
						}
						if(data[key].type == 2){
							_this.has.lunch = true
						}
						if(data[key].type == 3){
							_this.has.dinner = true
						}
					}

					_this.data = mylist.getData();
					_this.currentDate = _this.week[weekDay-1];


				},
				error: function(){
					//alert('网络错误，请重试');
				}
			})
		},
		buy: function(pra){
			if(this.request){
				this.request.abort();
			}

			var _this = this;
			var flag = pra.check.checked;
			//this.$broadcast('product-unbuy', '');

			if(flag == false){
				//alert('数量不能为0，如果想移除商品请前往购物删除');
				pra.check.setChecked(true);

			} else {
				pra.check.setChecked(true);
				this.request = $.ajax({
					url: SITE_URL + '/shoppingcart/addtocart',
					data: {
						productid: pra.id,
						amount: 1,
						date: _this.currentDate,
						type: pra.data.type
					},
					type: 'post',
					success: function(data){

						if(data.code){
							_this.total = data.data;
							_this.loading(_this.currentWeekDay)

						} else {
							pra.check.setChecked(false);
							dialog.init('友情提醒', data.msg).ok(function(){
								return true;
							});

						}

					},
					error: function(){
						//alert('网络错误，请重试');
					}

				});
			}


		},
		gotoshp: function(){
			window.location = SITE_URL + '/shoppingcart'
		}
	}
})
